package restless.core {
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import restless.utilities.RLEStringUtils;
	
	/**
	 * @author Jordan Laine
	 */
	
	public class RLEVisualElement extends MovieClip {
		
		private var tempMC:MovieClip;
		private var tempPoint:Point = new Point();
		private var tempFAP:RLEFlashAnimationPoint;
		private var bInitPoints:Boolean = false;
		private var aTrack:Array = [];
		private var vFAP:Vector.<RLEFlashAnimationPoint> = new Vector.<RLEFlashAnimationPoint>;
		
		protected var dupPoints:Boolean = false;
		
		public function parseMC(smoothing:Boolean = false):Vector.<BitmapData> {
			var i:int;
			var vImages:Vector.<BitmapData> = new Vector.<BitmapData>(totalFrames);
			for (i = 0; i < totalFrames; ++i) {
				gotoAndStop(i);
				if (aTrack.length > 0) {
					trackPoints();
				}
				var tempBitmapData:BitmapData = new BitmapData(width, height, true, 0x00000000);
				tempPoint.x = width;
				tempPoint.y = height;
				tempBitmapData.lock();
				tempBitmapData.draw(this, null, null, null, null, smoothing);
				tempBitmapData.unlock();
				vImages[i] = tempBitmapData;
			}
			
			return vImages;
		}
		
		public function trackPoints():void {
			var i:int;
			var tempString:String;
			if (!bInitPoints) {
				for (i = 0; i <= aTrack.length - 1; i++) {
					tempMC = aTrack[i];
					tempFAP = new RLEFlashAnimationPoint;
					tempString = String(tempMC);
					tempFAP.id = tempString.substring(8, tempString.length-1);
					tempFAP.parentID = String(this);
					tempFAP.index = i;
					vFAP.push(tempFAP);
				}
				bInitPoints = true;
			}
			
			for (i = 0; i <= aTrack.length - 1; i++) {
				tempMC = aTrack[i];
				tempFAP = vFAP[i];
				tempPoint = new Point(tempMC.x, tempMC.y);
				if (!dupPoints) {
					if (tempPoint == tempFAP.lastPoint) {
						trace("returned before point was added");
						return;
					}
				}
				tempFAP.addPoint(tempPoint);
			}
		}
		
		public function set trackObjs(movieClips:Array):void {
			var i:int = movieClips.length -1;
			for (i; i > -1; i--) {
				aTrack.push(movieClips.pop());
			}
		}
		
		public function get points():Vector.<RLEFlashAnimationPoint> {
			
			return vFAP;
		}
	}
}